<template>
  <div>
    <nav-bar></nav-bar>
    <div class="newGoods">
      <div class="banner">
        <img
          src="http://yanxuan.nosdn.127.net/8976116db321744084774643a933c5ce.png"
          alt=""
        />
      </div>
      <div class="nav">
        <div
          v-for="(item, index) in chooseList"
          :key="index"
          @click="choose(index)"
          :class="{
            active: index == current,
            desc: index === 1 && flag,
            asc: index === 1 && flag == false,
            price: index === 1,
          }"
        >
          {{ item }}
        </div>
      </div>
      <div class="goodList">
        <div
          class="item"
          v-for="item in showList"
          :key="item.id"
          @click="goGoods(item.id)"
        >
          <img :src="item.list_pic_url" alt="" />
          <p class="name">{{ item.name }}</p>
          <p class="price">￥{{ item.retail_price }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Navbar from "../../../components/Navbar";
import { goodsList } from "@/api/home/goods/index.js";

export default {
  props: {},
  data() {
    return {
      chooseList: ["综合", "价格", "分类"],
      current: 0,
      showList: [],
      flag: true,
    };
  },
  computed: {},
  created() {
    this.init();
  },
  mounted() {},
  watch: {},
  methods: {
    init() {
      goodsList({
        isHot: "1",
        isNew: "1",
        order: "asc",
      }).then((res) => {
        console.log(res);
        this.showList = res.data;
      });
    },
    inits() {
      goodsList({
        isHot: "1",
        isNew: "",
        order: "desc",
      }).then((res) => {
        console.log(res);
        this.showList = res.data;
      });
    },
    choose(index) {
      this.current = index;
      if (index == 1) {
        this.flag = !this.flag;
      }
      if (index == 1 && this.flag) {
        this.init();

        console.log(this.flag);
      } else if (index == 1 && !this.flag) {
        this.showList = [];
        this.inits();
      } else if (index == 0 || index == 2) {
        this.showList = [];
        this.init();
      }
    },
    // 跳转商品详情
    goGoods(id) {
      this.$router.push({ path: "/home/goods", query: { ids: id } });
    },
  },

  components: {
    "nav-bar": Navbar,
  },
};
</script>

<style lang="scss" scoped>
.banner {
  width: 100%;
  height: 140px;
  img {
    width: 100%;
    height: 100%;
  }
}
.nav {
  display: flex;
  background: #fff;
  width: 100%;
  height: 40px;
  line-height: 40px;
  div {
    width: 125px;
    height: 100%;
    text-align: center;
  }
  .active {
    color: #b4282d;
  }
}
.goodList {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .item {
    width: 186px;
    margin-bottom: 2px;
    text-align: center;
    background: #fff;
    padding: 7px 0;
    img {
      display: block;
      width: 151px;
      height: 151px;
      margin: 0 auto;
    }
    .name {
      margin: 7px 0 11px 0;
      text-align: center;
      padding: 0 10px;
      font-size: 12px;
    }
    .price {
      text-align: center;
      font-size: 15px;
      color: #b4282d;
    }
  }
}
.active.desc {
  background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/down-95e035f3e5.png)
    2.2rem no-repeat;
  background-size: 0.2rem 0.28rem;
}
.active.asc {
  background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/up-636b92c0a5.png)
    2.2rem no-repeat;
  background-size: 0.2rem 0.28rem;
}
.price {
  background: url(//yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/no-3127092a69.png)
    2.2rem no-repeat;
  background-size: 0.2rem 0.28rem;
}
</style>
